enum TEXTCOLOR {
    //% block="Black"
    ST7735_BLACK,
    //% block="Cyan"
    ST7735_CYAN,
    //% block="Yellow"
    ST7735_YELLOW,
    //% block="Green "
    ST7735_GREEN,
    //% block="Blue"
    ST7735_BLUE,
    //% block="Red"
    ST7735_RED,
    //% block="Magenta"
    ST7735_MAGENTA,
    //% block="White"
    ST7735_WHITE
}

enum DIRECTION {
    //% block="Up"
    0,
    //% block="Right"
    1,
    //% block="Down"
    2,
    //% block="Left"
    3
}


//% color="#A0522D" iconWidth=50 iconHeight=40
namespace TFT18 {

    //% block="Initialization" blockType="command"
    export function initialization(parameter: any, block: any) {
        Generator.addInclude('TFT18_GFX', '#include <Adafruit_GFX.h>');
        Generator.addInclude('TFT18_ST7735', '#include <Adafruit_ST7735.h>');
        Generator.addInclude('TFT18_SPI', '#include <SPI.h>');
        Generator.addInclude('TFT18_define_sclk', '#define sclk 13');
        Generator.addInclude('TFT18_define_mosi', '#define mosi 11');
        Generator.addInclude('TFT18_define_cs', '#define cs   10');
        Generator.addInclude('TFT18_define_dc', '#define dc   8');
        Generator.addInclude('TFT18_define_rst', '#define rst  5');
        Generator.addObject(`TFT18_define_object`, `Adafruit_ST7735`, `tft = Adafruit_ST7735(cs, dc, mosi, sclk, rst);`);
        Generator.addSetup(`TFT18_Init`, `tft.initR(INITR_BLACKTAB);`);
    }

    //% block="Clear Screen" blockType="command"
    export function clearScreen(parameter: any, block: any) {
        Generator.addCode(`tft.fillScreen(ST7735_BLACK);`);
    }

    //% block="set the screen direction to [DIRECTION]" blockType="command"
    //% DIRECTION.shadow="dropdown" DIRECTION.options="DIRECTION" DIRECTION.defl="DIRECTION.0"
    export function setDirection(parameter: any, block: any) {
        let direction = parameter.DIRECTION.code
        Generator.addCode(`tft.setRotation(${direction});`);
    }

    //% block="set the text size to [TEXTSIZE]" blockType="command"
    //% TEXTSIZE.shadow="range" TEXTSIZE.params.min=1 TEXTSIZE.params.max=12 TEXTSIZE.defl=3
    export function setFontSize(parameter: any, block: any) {
        let textSize = parameter.TEXTSIZE.code
        Generator.addCode(`tft.setTextSize(${textSize});`);
    }

/*
    //% block="use number to set the text color to [TEXTCOLORBYNUMBER]" blockType="command"
    //% TEXTCOLORBYNUMBER.shadow="number" TEXTCOLORBYNUMBER.defl="123"
    export function setFontColorByNumber(parameter: any, block: any) {
        let textColorByNumber = parameter.TEXTCOLORBYNUMBER.code
        Generator.addCode(`tft.setTextColor(${textColorByNumber});`);
    }
*/

    //% block="set the text color to [TEXTCOLOR]" blockType="command"
    //% TEXTCOLOR.shadow="dropdown" TEXTCOLOR.options="TEXTCOLOR" TEXTCOLOR.defl="TEXTCOLOR.ST7735_WHITE"
    export function setFontColor(parameter: any, block: any) {
        let textColor = parameter.TEXTCOLOR.code
        Generator.addCode(`tft.setTextColor(${textColor});`);
    }

//tft.setCursor(0, 60);
    //% block="Set Cursor at x [X] y [Y]" blockType="command"
    //% X.shadow="range" X.params.min=0 X.params.max=127 X.defl=0
    //% Y.shadow="range" Y.params.min=0 Y.params.max=159 Y.defl=0
    export function setCursor(parameter: any, block: any) {
        let x = parameter.X.code
        let y = parameter.Y.code
        Generator.addCode(`tft.setCursor(${x}, ${y});`);
    }

    //% block="show [STR] with return" blockType="command"
    //% STR.shadow="string" STR.defl=hello
    export function printTextln(parameter: any, block: any) {
        let str = parameter.STR.code
        Generator.addCode(`tft.println(${str});`);
    }

    //% block="draw solid circle at x [X] y [Y] r [R] color [COLOR]" blockType="command"
    //% X.shadow="range" X.params.min=0 X.params.max=127 X.defl=63
    //% Y.shadow="range" Y.params.min=0 Y.params.max=159 Y.defl=79
    //% R.shadow="range" R.params.min=1 Y.params.max=160 R.defl=10
    //% COLOR.shadow="dropdown" COLOR.options="TEXTCOLOR" COLOR.defl="TEXTCOLOR.ST7735_WHITE"
    export function drawSolidCircle(parameter: any, block: any) {
        let x = parameter.X.code
        let y = parameter.Y.code
        let r = parameter.R.code
		let color = parameter.COLOR.code
        Generator.addCode(`tft.fillCircle(${x}, ${y}, ${r}, ${color});`);
    }

    //% block="draw hollow circle at x [X] y [Y] r [R] color [COLOR]" blockType="command"
    //% X.shadow="range" X.params.min=0 X.params.max=127 X.defl=63
    //% Y.shadow="range" Y.params.min=0 Y.params.max=159 Y.defl=79
    //% R.shadow="range" R.params.min=1 Y.params.max=160 R.defl=10
    //% COLOR.shadow="dropdown" COLOR.options="TEXTCOLOR" COLOR.defl="TEXTCOLOR.ST7735_WHITE"
    export function drawHollowCircle(parameter: any, block: any) {
        let x = parameter.X.code
        let y = parameter.Y.code
        let r = parameter.R.code
		let color = parameter.COLOR.code
        Generator.addCode(`tft.drawCircle(${x}, ${y}, ${r}, ${color});`);
    }

    //% block="draw triangle at x0 [X0] y0 [Y0] x1 [X1] y1 [Y1] x2 [X2] y2 [Y2] color [COLOR]" blockType="command"
    //% X0.shadow="range" X0.params.min=0 X0.params.max=127 X0.defl=0
    //% Y0.shadow="range" Y0.params.min=0 Y0.params.max=159 Y0.defl=0
    //% X1.shadow="range" X1.params.min=0 X1.params.max=127 X1.defl=127
    //% Y1.shadow="range" Y1.params.min=0 Y1.params.max=159 Y1.defl=0
    //% X2.shadow="range" X2.params.min=0 X2.params.max=127 X0.defl=0
    //% Y2.shadow="range" Y2.params.min=0 Y2.params.max=159 Y0.defl=159
    //% COLOR.shadow="dropdown" COLOR.options="TEXTCOLOR" COLOR.defl="TEXTCOLOR.ST7735_GREEN"
    export function drawTriangle(parameter: any, block: any) {
        let x0 = parameter.X0.code
        let y0 = parameter.Y0.code
        let x1 = parameter.X1.code
        let y1 = parameter.Y1.code
        let x2 = parameter.X2.code
        let y2 = parameter.Y2.code
		let color = parameter.COLOR.code
        Generator.addCode(`tft.drawTriangle(${x0}, ${y0}, ${x1}, ${y1}, ${x2}, ${y2}, ${color});`);
    }

    //% block="draw hollow rectangle from x [X] y [Y] width [W] height [H] color [COLOR]" blockType="command"
    //% X.shadow="range" X.params.min=0 X.params.max=159 X.defl=0
    //% Y.shadow="range" Y.params.min=0 Y.params.max=159 Y.defl=0
    //% W.shadow="range" W.params.min=0 W.params.max=159 W.defl=10
    //% H.shadow="range" H.params.min=0 H.params.max=159 H.defl=10
    //% COLOR.shadow="dropdown" COLOR.options="TEXTCOLOR" COLOR.defl="TEXTCOLOR.ST7735_BLUE"
    export function drawRectangle(parameter: any, block: any) {
        let x = parameter.X.code
        let y = parameter.Y.code
        let w = parameter.W.code
        let h = parameter.H.code
		let color = parameter.COLOR.code
        Generator.addCode(`tft.drawRect(${x}, ${y}, ${w}, ${h}, ${color});`);
    }

    //% block="draw solid rectangle from x [X] y [Y] width [W] height [H] color [COLOR]" blockType="command"
    //% X.shadow="range" X.params.min=0 X.params.max=159 X.defl=0
    //% Y.shadow="range" Y.params.min=0 Y.params.max=159 Y.defl=0
    //% W.shadow="range" W.params.min=0 W.params.max=159 W.defl=10
    //% H.shadow="range" H.params.min=0 H.params.max=159 H.defl=10
    //% COLOR.shadow="dropdown" COLOR.options="TEXTCOLOR" COLOR.defl="TEXTCOLOR.ST7735_BLUE"
    export function drawSolidRectangle(parameter: any, block: any) {
        let x = parameter.X.code
        let y = parameter.Y.code
        let w = parameter.W.code
        let h = parameter.H.code
		let color = parameter.COLOR.code
        Generator.addCode(`tft.fillRect(${x}, ${y}, ${w}, ${h}, ${color});`);
    }


    //% block="draw line at x0 [X0] y0 [Y0] x1 [X1] y1 [Y1] color [COLOR]" blockType="command"
    //% X0.shadow="range" X0.params.min=0 X0.params.max=127 X0.defl=0
    //% Y0.shadow="range" Y0.params.min=0 Y0.params.max=159 Y0.defl=0
    //% X1.shadow="range" X1.params.min=0 X1.params.max=127 X1.defl=127
    //% Y1.shadow="range" Y1.params.min=0 Y1.params.max=159 Y1.defl=159
    //% COLOR.shadow="dropdown" COLOR.options="TEXTCOLOR" COLOR.defl="TEXTCOLOR.ST7735_BLUE"
    export function drawLine(parameter: any, block: any) {
        let x0 = parameter.X0.code
        let y0 = parameter.Y0.code
        let x1 = parameter.X1.code
        let y1 = parameter.Y1.code
		let color = parameter.COLOR.code
        Generator.addCode(`tft.drawLine(${x0}, ${y0}, ${x1}, ${y1}, ${color});`);
    }

    //% block="draw pixel at x [X] y [Y] color [COLOR]" blockType="command"
    //% X.shadow="range" X.params.min=0 X.params.max=127 X.defl=63
    //% Y.shadow="range" Y.params.min=0 Y.params.max=159 Y.defl=79
    //% COLOR.shadow="dropdown" COLOR.options="TEXTCOLOR" COLOR.defl="TEXTCOLOR.ST7735_RED"
    export function drawPixel(parameter: any, block: any) {
        let x = parameter.X.code
        let y = parameter.Y.code
		let color = parameter.COLOR.code
        Generator.addCode(`tft.drawPixel(${x}, ${y}, ${color});`);
    }

}
